$(function () {
    $("#kcpd_detail").edatagrid({
        url:$.webPath() + "/kcgl/kcpd/kcpd_pdmx?kcpdId=" + $("#kcpd_pdmx_id").val(),
        fitColumns:true,
        singleSelect:true,
        fit:true,
        selectOnCheck:false,
        rownumbers:true,
        columns:[
            [
                {field:'kh.khbm', title:'卡号', width:35},
                {field:'wzgg.code', title:'物资编码', width:40, align:'center'},
                {field:'wzgg.name', title:'物资名称', width:30, align:'center'},
                {field:'wzgg.ggxh', title:'规格型号', width:30, align:'center'},
                {field:'wzgg.cz', title:'材质', width:30, align:'center'},
                {field:'cd.z', title:'产地', width:20, align:'center'},
                {field:'wzgg.dw', title:'单位', width:20, align:'center'},
                {field:'wsdj', title:'无税单价', width:30, align:'right',
                    formatter:function (value) {
                        var val = formatJeForHtml(value);
                        return val;
                    }},
                {field:'zmsl', title:'账面数量', width:30, align:'right',
                    formatter:function (value) {
                        var val = formatSlForHtml(value);
                        return val;
                    }},
                {field:'spsl', title:'实盘数量', align:'right', width:30, editor:{type:'numberbox', options:{groupSeparator:',', precision:4}},
                    formatter:function (value) {
                        var val = formatSlForHtml(value);
                        return val;
                    }},
                {field:'yksl', title:'盈亏数量', width:30, align:'right',
                    formatter:function (value) {
                        var val = formatSlForHtml(value);
                        return val;
                    }},
                {field:'ykje', title:'盈亏金额(元)', width:40, align:'right',
                    formatter:function (value) {
                        var val = formatJeForHtml(value);
                        return val;
                    }}
            ]
        ],
        onLoadSuccess:function () {
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        onSelect:function (rowIndex, rowData) {
            //获取每个单元格
            $("#kcpd_detail").edatagrid('editRow', rowIndex);
            var editors = $("#kcpd_detail").datagrid('getEditors', rowIndex);
            if (editors.length == 0)
                return;
            var spslEditor = editors[0];

            spslEditor.target.unbind("blur").bind('blur', function () {
                $(spslEditor.target).numberbox('setValue', $(spslEditor.target).val());
                rowData.spsl = $(spslEditor.target).numberbox('getValue');
                rowData.yksl = rowData.spsl - rowData['zmsl'];
                rowData.ykje = rowData.yksl * rowData['wsdj'];
                $("#kcpd_detail").edatagrid('refreshRow', rowIndex);
            });
        },
        rowStyler:function (rowIndex, rowData) {
            if (rowData.ykje < 0) {
                return 'background-color:rgba(67, 110, 238, 0.64);';
            } else if (rowData.ykje > 0) {
                return 'background-color:rgba(255, 99, 71, 0.64);';
            } else if (rowData.ykje == 0) {
                return 'background-color:rgba(84, 255, 159, 0.64);';
            }
        },
        loadFilter:function (data) {
            $.each(data.rows, function () {
                this.spsl = this.zmsl ? this.zmsl : 0;
                this.yksl = this.spsl - this.zmsl ? this.zmsl : 0;
                this.ykje = this.yksl * this.wsdj;
            })
            return data;
        }
    })
});